Object.defineProperty(exports, "__esModule", { value: !0 }),
  (exports.default = void 0);
var e = require("../../@babel/runtime/helpers/regeneratorRuntime"),
  r = require("../../@babel/runtime/helpers/asyncToGenerator"),
  t = require("../../@babel/runtime/helpers/classCallCheck"),
  n = require("../../@babel/runtime/helpers/createClass"),
  a = require("./sensor-1.0.1.min").generateAdvertisingData,
  i = new ((function () {
    function i(e) {
      var r = this;
      t(this, i),
        (this.started = !1),
        (this.server = null),
        (this.mac = null),
        (this.reopenTimer = void 0),
        (this.delayStartTimer = void 0),
        (this.powerLevel = "high"),
        (this.androidAdvCount = 3),
        (this.advTime = 0),
        (this.sleepTime = 100),
        (this._onBeaconServiceChangeHandler = function (e) {
          r.started &&
            (console.info("_onBeaconServiceChangeHandler", e),
            e.available && (r.stop(), r.start()));
        });
    }
    var s;
    return (
      n(i, [
        {
          key: "setMac",
          value: function (e) {
            this.mac = "0x" + e;
          },
        },
        {
          key: "innerAdvData",
          value: function (e, r) {
            if ("ios" === wx.getSystemInfoSync().platform)
              e.startAdvertising(r);
            else
              for (var t = 0; t < this.androidAdvCount; t++)
                e.startAdvertising(r);
          },
        },
        {
          key: "createServer",
          value: function () {
            var t = this;
            clearTimeout(this.reopenTimer),
              this.started &&
                (wx.onBluetoothAdapterStateChange(
                  this._onBeaconServiceChangeHandler,
                ),
                clearTimeout(this.delayStartTimer),
                (this.delayStartTimer = setTimeout(function () {
                  t.started &&
                    wx.createBLEPeripheralServer({
                      success: function (n) {
                        var i;
                        (t.server = n.server),
                          wx.getSystemInfo({
                            success:
                              ((i = r(
                                e().mark(function r(n) {
                                  var i, s;
                                  return e().wrap(function (e) {
                                    for (;;)
                                      switch ((e.prev = e.next)) {
                                        case 0:
                                          if (t.started) {
                                            e.next = 2;
                                            break;
                                          }
                                          return e.abrupt("return");
                                        case 2:
                                          if (
                                            ((i = t.mac),
                                            console.info(i, "macId"),
                                            ((s = a(n.platform, {
                                              id: i,
                                              battery: 8,
                                              alarm: !1,
                                            })).powerLevel = t.powerLevel),
                                            console.info(
                                              "蓝牙包",
                                              s.powerLevel,
                                            ),
                                            console.info(s),
                                            !(t.advTime > 0))
                                          ) {
                                            e.next = 25;
                                            break;
                                          }
                                          console.info(
                                            "开关模式",
                                            t.advTime,
                                            t.sleepTime,
                                          );
                                        case 10:
                                          if (!t.started) {
                                            e.next = 23;
                                            break;
                                          }
                                          return (
                                            t.innerAdvData(t.server, s),
                                            (e.next = 14),
                                            t.sleep(t.advTime)
                                          );
                                        case 14:
                                          if (!t.started) {
                                            e.next = 18;
                                            break;
                                          }
                                          t.server.stopAdvertising(),
                                            (e.next = 19);
                                          break;
                                        case 18:
                                          return e.abrupt("break", 23);
                                        case 19:
                                          return (
                                            (e.next = 21), t.sleep(t.sleepTime)
                                          );
                                        case 21:
                                          e.next = 10;
                                          break;
                                        case 23:
                                          e.next = 27;
                                          break;
                                        case 25:
                                          console.info("常开模式"),
                                            t.innerAdvData(t.server, s);
                                        case 27:
                                        case "end":
                                          return e.stop();
                                      }
                                  }, r);
                                }),
                              )),
                              function (e) {
                                return i.apply(this, arguments);
                              }),
                            fail: function (e) {
                              console.error(e);
                            },
                          });
                      },
                      fail: function (e) {
                        console.error(e);
                      },
                    });
                }, 20)));
          },
        },
        {
          key: "sleep",
          value:
            ((s = r(
              e().mark(function r(t) {
                return e().wrap(function (e) {
                  for (;;)
                    switch ((e.prev = e.next)) {
                      case 0:
                        return e.abrupt(
                          "return",
                          new Promise(function (e) {
                            return setTimeout(e, t);
                          }),
                        );
                      case 1:
                      case "end":
                        return e.stop();
                    }
                }, r);
              }),
            )),
            function (e) {
              return s.apply(this, arguments);
            }),
        },
        {
          key: "start",
          value: function () {
            var e = this;
            this.mac
              ? ((this.started = !0),
                clearTimeout(this.reopenTimer),
                (this.reopenTimer = setTimeout(function () {
                  e.start();
                }, 2e3)),
                wx.openBluetoothAdapter({
                  mode: "peripheral",
                  success: function (r) {
                    e.createServer();
                  },
                  fail: function (r) {
                    (-1 !== r.errCode &&
                      "openBluetoothAdapter:fail already opened" !==
                        r.errMsg) ||
                      e.createServer(),
                      console.error(r);
                  },
                }))
              : wx.showToast({ title: "mac不存在，无法启动aoa", icon: "none" });
          },
        },
        {
          key: "stop",
          value: function () {
            (this.started = !1),
              clearTimeout(this.reopenTimer),
              clearTimeout(this.delayStartTimer),
              wx.offBluetoothAdapterStateChange(
                this._onBeaconServiceChangeHandler,
              ),
              null != this.server &&
                (this.server.stopAdvertising(),
                this.server.close(),
                (this.server = null));
          },
        },
      ]),
      i
    );
  })())();
exports.default = i;
